﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    /// <summary>
    /// 2.应收帐款销售类型更改记录表(FTradeSalesType)
    /// </summary>
    public class FTradeSalesTypeDAL
    {
        /// <summary>
        /// 获取所有历史的销售类型记录
        /// </summary>
        /// <param name="FTradeID"></param>
        /// <returns></returns>
        public static IDataReader GetAllByFTradeID(int FTradeID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeSalesType_GetAllByFTradeID", FTradeID);
            return DB.ExecuteReader(dbCommand);
        }
        /// <summary>
        /// 获取明细
        /// </summary>
        /// <param name="FTradeSalesId"></param>
        /// <returns></returns>
        public static IDataReader GetByFTradeSalesId(int FTradeSalesId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeSalesType_GetByFTradeSalesId", FTradeSalesId);
            return DB.ExecuteReader(dbCommand);
        }
        ///// <summary>
        ///// 变更应收帐款销售类型壮态
        ///// 将　@FTradeSalesId　对应的所有类型记录状态改成　取消
        ///// 取削　帐龄明细 记录
        ///// 固此存储过程需在变更状态前执行
        ///// </summary>
        ///// <param name="FTradeSalesId"></param>
        ///// <returns></returns>
        //public static bool UpdateState(int FTradeSalesId)
        //{
        //    Database DB = DatabaseFactory.CreateDatabase();

        //    DbCommand dbCommand = DB.GetStoredProcCommand("FTradeSalesType_UpdateState", FTradeSalesId);
        //    try
        //    {
        //        DB.ExecuteScalar(dbCommand);
        //        return true;
        //    }
        //    catch (Exception exc)
        //    {
        //        return false;
        //    }
        //}
        ///// <summary>
        ///// 变更销售类型
        ///// 1.如将全额改成短信分期等.
        ///// 改变后,保留原有的销售类型信息
        ///// 
        ///// 如果原有的销售类型已充值过,则必需先进行调帐,才能变更
        ///// </summary>
        ///// <param name="FTradeID"></param>
        ///// <param name="OldFTradeSalesId">原来的销售类型ID</param>
        ///// <param name="objSalesType">新的销售类型</param>
        ///// <param name="listAgeDetails">对应的帐龄明细</param>
        ///// <param name="haveBalance">冲帐记录数</param>
        ///// <param name="objCat">如果冲过帐,则需添加新的收款记录</param>
        ///// <param name="FbalanceIds">原冲帐记录ID</param>
        ///// <returns></returns>
        //public static List<string> UpdateSaleType(int FTradeID,int OldFTradeSalesId , FTradeSalesTypeInfo objSalesType, List<FTradeAgeDetailInfo> listAgeDetails,int haveBalance,FTradeCatheringInfo objCat,List<int> FbalanceIds)
        //{
        //    Database DB = DatabaseFactory.CreateDatabase();
        //    using (DbConnection conn = DB.CreateConnection())
        //    {
        //        //打开连接
        //        conn.Open();
        //        //创建事务
        //        DbTransaction trans = conn.BeginTransaction();
        //        try
        //        {
        //            int NewFCatheringId = 0;
        //            //是否有冲过帐处理
        //            if (haveBalance>0)
        //            {
        //                DbCommand dbCommandCather = DB.GetStoredProcCommand("FTradeCathering_Add", objCat.FCTitle, objCat.ShoukuanRiQi, objCat.JingXiaoShang, objCat.JinE, objCat.KuanXiangLeiXing, objCat.RealName, objCat.IsAdjustable, objCat.State, objCat.Remark, objCat.SourceTag);
        //                NewFCatheringId = Convert.ToInt32(DB.ExecuteScalar(dbCommandCather, trans));

        //                foreach (int FbalanceId in FbalanceIds)
        //                {
        //                    //2
        //                    DbCommand dbCommand = DB.GetStoredProcCommand("FTradeBalance_AddAdjusting", FbalanceId, NewFCatheringId);
        //                    DB.ExecuteScalar(dbCommand, trans);
        //                }
        //            }

        //            //取消原有的销售类型的状态
        //            DbCommand UpdateStatedbCommand = DB.GetStoredProcCommand("FTradeSalesType_UpdateState", OldFTradeSalesId);
        //            DB.ExecuteNonQuery(UpdateStatedbCommand, trans);                    
                   
        //            //2.应收帐款销售类型更改记录表
        //            DbCommand objSalesTypedbCommand = DB.GetStoredProcCommand("FTradeSalesType_Add", FTradeID, objSalesType.XiaoShouLeiXing, objSalesType.ExConfig, objSalesType.State, objSalesType.RealName);
        //            int FTradeSalesId = Convert.ToInt32(DB.ExecuteScalar(objSalesTypedbCommand, trans));
        //            //3.应收账款帐龄明细表(FTradeAgeDetail)
        //            foreach (FTradeAgeDetailInfo objAgeDetail in listAgeDetails)
        //            {
        //                DbCommand objAgeDetaildbCommand = DB.GetStoredProcCommand("", FTradeSalesId, objAgeDetail.ShowIndex, objAgeDetail.TongJiName, objAgeDetail.ShowTitle, objAgeDetail.JinE, objAgeDetail.DaoQiRi, objAgeDetail.SuspendedMark, objAgeDetail.Remark, objAgeDetail.RealName);
        //                int FTradeAgeId = Convert.ToInt32(DB.ExecuteScalar(objAgeDetaildbCommand, trans));
        //            }

        //            trans.Commit();
        //            return new List<string>() { "", NewFCatheringId.ToString() };
        //        }
        //        catch (Exception exc)
        //        {
        //            //发生异常，事务回滚
        //            trans.Rollback();
        //            return new List<string>() { exc.Message, "0" };
        //        }
        //        finally
        //        {
        //            //关闭连接
        //            conn.Close();
        //        }
        //    }
        //}

       
    }
}
